package com.cheetah.start.system.role.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cheetah.start.common.annotation.ApiCount;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.EnableEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.common.enums.RoleEnum;
import com.cheetah.start.system.role.domain.SysRole;
import com.cheetah.start.system.role.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统角色控制器
 *
 * @author Feel
 * @date 2021/09/05
 */
@Api(tags = "角色管理")
@Slf4j
@ApiCount
@CrossOrigin
@Validated
@RestController
@RequestMapping("/role")
public class SysRoleController extends ResultController {

    @Autowired
    private SysRoleService roleService;


    /**
     * 角色页面
     *
     * @return {@link ModelAndView}
     */
    @SaCheckPermission(value = "sys:role:main",orRole = "admin")
    @ApiOperation(value = "角色页面")
    @GetMapping("/view")
    public ModelAndView view() {
        return jumpPage("/role/list");
    }

    /**
     * 添加页面
     *
     * @return {@link ModelAndView}
     */
    @ApiOperation(value = "角色添加页面")
    @GetMapping("/add")
    public ModelAndView add() {
        return jumpPage("/role/add");
    }

    /**
     * 编辑页面
     *
     * @param modelAndView 模型和视图
     * @param roleId       角色id
     * @return {@link ModelAndView}
     */
    @ApiOperation("角色编辑页面")
    @GetMapping("/edit")
    public ModelAndView edit(ModelAndView modelAndView, Integer roleId) {
        SysRole role = roleService.getById(roleId);
        modelAndView.addObject("sysRole", role);
        modelAndView.setViewName("/role/edit");
        return modelAndView;
    }


    /**
     * 角色权限页面
     *
     * @return {@link ModelAndView}
     */
    @ApiOperation("获取授权")
    @GetMapping("/power")
    public ModelAndView power(Integer roleId, ModelAndView modelAndView) {
        modelAndView.addObject("roleId", roleId);
        modelAndView.setViewName("/role/power");
        return modelAndView;
    }


    /**
     * 启用
     *
     * @param roleId 角色id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:switch",orRole = "admin")
    @ApiOperation("启用")
    @Log(title = "角色", describe = "启用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    @PostMapping("/enable")
    public Result enable(@NotNull(message = "ID不能为空") Integer roleId) {
        SysRole role = roleService.getById(roleId);
        role.setEnable(EnableEnum.ENABLE.getCode());
        boolean b = roleService.updateById(role);
        return decide(b, BusinessTypeEnum.EDIT);
    }

    /**
     * 禁用
     *
     * @param roleId 角色id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:switch",orRole = "admin")
    @ApiOperation("禁用")
    @Log(title = "角色", describe = "禁用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    @PostMapping("/disable")
    public Result disable(@NotNull(message = "ID不能为空") Integer roleId) {
        SysRole role = roleService.getById(roleId);
        role.setEnable(EnableEnum.DISABLE.getCode());
        if (roleService.isSuperAdminDisable(role)) {
            return failure("超级管理员不能禁用");
        }
        boolean b = roleService.updateById(role);
        return decide(b, BusinessTypeEnum.EDIT);
    }


    /**
     * 所有的角色
     *
     * @return {@link Result}
     */
    @GetMapping("allRole")
    public Result allRole(){
        List<SysRole> sysRoles = roleService.listEnable();
        return success(sysRoles);
    }
}
